﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Wild2.Models.Exceptions;
using Wild2.Models.User;
using System.Text.RegularExpressions;

namespace Wild2.Controllers
{

    public class BlogController : Controller
    {
        //
        // GET: /Blog/

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Add()
        {
            Models.Pages.Blog.Add page = new Models.Pages.Blog.Add();
            Int32? UserID = Wild2.Models.Auth.AuthOnSite.GetUserId();
            if (!UserID.HasValue) return RedirectToAction("Index", "Login");
            return View(page);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Add(String caption, String text, String longtext, String tags, bool isPublic, bool hidecomments)
        {
            Models.Pages.Blog.Add page = new Models.Pages.Blog.Add();
            Int32? UserID = Wild2.Models.Auth.AuthOnSite.GetUserId();
            if (!UserID.HasValue) return RedirectToAction("Index", "Login");
            try
            {
                if (Wild2.Models.Text.Helper.GetLastMessage() != caption)
                {
                    Models.Blog.Blog.AddTopic(UserID.Value, caption, text, longtext, tags.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries), !isPublic, hidecomments);
                    Wild2.Models.Text.Helper.SetLastMessage(caption);
                    return RedirectToAction("My", "Blog");
                }
                else
                {
                    page.infoMessage = "Это сообщение уже отправлено";
                }
            }
            catch (TextException e) 
            {
                page.infoMessage = e.Message;
            }
            return View(page);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult AddSandbox()
        {
            Models.Pages.Blog.AddSandbox page = new Models.Pages.Blog.AddSandbox();
            return View(page);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult AddSandbox(String caption, String text, String nick, String email)
        {
            Models.Pages.Blog.AddSandbox page = new Models.Pages.Blog.AddSandbox();
            try
            {
                if (Wild2.Models.Text.Helper.GetLastMessage() != caption)
                {
                    Int32? UserID = Wild2.Models.Auth.AuthOnSite.GetUserId();
                    if (!UserID.HasValue && (String.IsNullOrEmpty(nick) || Wild2.Models.Auth.RegisterOnSite._reg.IsMatch(nick))) 
                    {
                        page.infoMessage = "Ник содержит запрещенные символы";
                    }
                    else
                    {
                        if (UserID.HasValue) nick = Wild2.Models.Auth.AuthOnSite.GetUserNick();
                        Models.Blog.Blog.AddTopicSandbox(UserID, caption, text, nick, email);
                        Wild2.Models.Text.Helper.SetLastMessage(caption);
                        return RedirectToAction("Sandbox", "Blog");
                    }
                }
                else
                {
                    page.infoMessage = "Это сообщение уже отправлено";
                }
            }
            catch (TextException e)
            {
                page.infoMessage = e.Message;
            }
            return View(page);
        }

        public ActionResult My(int? parameter)
        {
            Models.Pages.Blog.My page = new Models.Pages.Blog.My();
            Int32? UserID = Wild2.Models.Auth.AuthOnSite.GetUserId();
            if (!UserID.HasValue) return RedirectToAction("Index", "Login");
            //******************************************************
            Models.Favorites.Favorites.AddFavoriteToSession("Мои записи");
            Wild2.Models.ViewTabs.ViewTabs.setCurrentTab(Url.RequestContext.RouteData.Values["controller"].ToString(), Url.RequestContext.RouteData.Values["action"].ToString());
            //******************************************************
            Wild2.Models.Pages.Controls.PagingTop pt = new Wild2.Models.Pages.Controls.PagingTop();
            pt.PreviousLink = Url.Action("Sandbox", "Blog", new { parameter = 0 });
            pt.PreviousName = "Песочница";
            pt.CurrentName = "Мои записи";
            pt.NextLink = Url.Action("Subscr", "Blog", new { parameter = 0 });
            pt.NextName = "Лента друзей";
            page.pt = pt;
            //******************************************************

            //******************************************************
            Wild2.Models.Pages.Controls.BlogsTopicList btl = new Models.Pages.Controls.BlogsTopicList();
            if (parameter.HasValue) btl.TopicList = Models.Blog.Blog.GetTopics(parameter.Value, UserID.Value);
            else btl.TopicList = Models.Blog.Blog.GetTopics(0, UserID.Value);
            page.btl = btl;
            //******************************************************

            //******************************************************
            Wild2.Models.Pages.Controls.PagingBottom pb = new Wild2.Models.Pages.Controls.PagingBottom();
            if (parameter.HasValue)
            {
                pb.PreviousLink = Url.Action("My", new { parameter = parameter + 1 });
                if (parameter > 0)
                {
                    pb.NextLink = Url.Action("My", new { parameter = parameter - 1 });
                }
            }
            else
            {
                pb.PreviousLink = Url.Action("My", new { parameter = 1 });
            }
            page.pb = pb;
            //******************************************************

            return View(page);
        }

        public ActionResult Subscr(int? parameter)
        {
            Models.Pages.Blog.Subscr page = new Models.Pages.Blog.Subscr();
            Int32? UserID = Wild2.Models.Auth.AuthOnSite.GetUserId();
            if (!UserID.HasValue) return RedirectToAction("Index", "Login");
            //******************************************************
            Models.Favorites.Favorites.AddFavoriteToSession("Лента друзей");
            Wild2.Models.ViewTabs.ViewTabs.setCurrentTab(Url.RequestContext.RouteData.Values["controller"].ToString(), Url.RequestContext.RouteData.Values["action"].ToString());
            //******************************************************
            Wild2.Models.Pages.Controls.PagingTop pt = new Wild2.Models.Pages.Controls.PagingTop();
            pt.PreviousLink = Url.Action("My", "Blog", new { parameter = 0 });
            pt.PreviousName = "Мои записи";
            pt.CurrentName = "Лента друзей";
            pt.NextLink = Url.Action("Index", "Home", new { parameter = 0 });
            pt.NextName = "Популярные";
            page.pt = pt;
            //******************************************************

            //******************************************************
            Wild2.Models.Pages.Controls.BlogsTopicList btl = new Models.Pages.Controls.BlogsTopicList();
            if (parameter.HasValue) btl.TopicList = Models.Blog.Blog.GetFriendsAndMyTopics(parameter.Value, UserID.Value);
            else btl.TopicList = Models.Blog.Blog.GetFriendsAndMyTopics(0, UserID.Value);
            page.btl = btl;
            //******************************************************

            //******************************************************
            Wild2.Models.Pages.Controls.PagingBottom pb = new Wild2.Models.Pages.Controls.PagingBottom();
            if (parameter.HasValue)
            {
                pb.PreviousLink = Url.Action("Subscr", new { parameter = parameter + 1 });
                if (parameter > 0)
                {
                    pb.NextLink = Url.Action("Subscr", new { parameter = parameter - 1 });
                }
            }
            else
            {
                pb.PreviousLink = Url.Action("Subscr", new { parameter = 1 });
            }
            page.pb = pb;
            //******************************************************

            return View(page);
        }

        public ActionResult User(int id, int? parameter)
        {
            Models.Pages.Blog.User page = new Models.Pages.Blog.User();
            Int32? UserID = Wild2.Models.Auth.AuthOnSite.GetUserId();
            //******************************************************
            Wild2.Models.Pages.Controls.PagingTop pt = new Wild2.Models.Pages.Controls.PagingTop();
            pt.PreviousLink = Url.Action("All", "Blog", new { parameter = 0 });

            pt.PreviousName = "Все записи";
            UserData userData = Wild2.Models.User.User.GetUserData(id);
            if (UserID.HasValue)
            {
                Models.Favorites.Favorites.AddFavoriteToSession("Записи пользователя " + userData.Nick);
            }
            pt.CurrentName = HttpUtility.HtmlEncode(userData.Nick);
            pt.NextLink = Url.Action("Index", "Home", new { parameter = 0 });
            pt.NextName = "Популярные";
            page.pt = pt;
            //******************************************************

            //******************************************************
            Wild2.Models.Pages.Controls.BlogsTopicList btl = new Models.Pages.Controls.BlogsTopicList();
            if (parameter.HasValue) btl.TopicList = Models.Blog.Blog.GetTopics(parameter.Value, id);
            else btl.TopicList = Models.Blog.Blog.GetTopics(0, id);
            page.btl = btl;
            //******************************************************

            //******************************************************
            Wild2.Models.Pages.Controls.PagingBottom pb = new Wild2.Models.Pages.Controls.PagingBottom();
            if (parameter.HasValue)
            {
                pb.PreviousLink = Url.Action("User", new { id = id, parameter = parameter + 1 });
                if (parameter > 0)
                {
                    pb.NextLink = Url.Action("User", new { id = id, parameter = parameter - 1 });
                }
            }
            else
            {
                pb.PreviousLink = Url.Action("User", new { id = id, parameter = 1 });
            }
            page.pb = pb;
            //******************************************************

            return View(page);
        }

        public ActionResult All(int? parameter)
        {
            Models.Pages.Blog.All page = new Models.Pages.Blog.All();
            Int32? UserID = Wild2.Models.Auth.AuthOnSite.GetUserId();
            if (UserID.HasValue)
            {
                Models.Favorites.Favorites.AddFavoriteToSession("Все записи");
                Wild2.Models.ViewTabs.ViewTabs.setCurrentTab(Url.RequestContext.RouteData.Values["controller"].ToString(), Url.RequestContext.RouteData.Values["action"].ToString());
            }
            //******************************************************
            Wild2.Models.Pages.Controls.PagingTop pt = new Wild2.Models.Pages.Controls.PagingTop();
            pt.PreviousLink = Url.Action("Index", "Home", new { parameter = 0 });
            pt.PreviousName = "Популярные";
            pt.CurrentName = "Все записи";
            pt.NextLink = Url.Action("Sandbox", "Blog", new { parameter = 0 });
            pt.NextName = "Песочница";
            page.pt = pt;
            //******************************************************

            //******************************************************
            Wild2.Models.Pages.Controls.BlogsTopicList btl = new Models.Pages.Controls.BlogsTopicList();
            if (parameter.HasValue) btl.TopicList = Models.Blog.Blog.GetTopics(parameter.Value);
            else btl.TopicList = Models.Blog.Blog.GetTopics();
            page.btl = btl;
            //******************************************************

            //******************************************************
            Wild2.Models.Pages.Controls.PagingBottom pb = new Wild2.Models.Pages.Controls.PagingBottom();
            if (parameter.HasValue)
            {
                pb.PreviousLink = Url.Action("All", new { parameter = parameter + 1 });
                if (parameter > 0)
                {
                    pb.NextLink = Url.Action("All", new { parameter = parameter - 1 });
                }
            }
            else
            {
                pb.PreviousLink = Url.Action("All", new { parameter = 1 });
            }
            page.pb = pb;
            //******************************************************

            return View(page);
        }

        public ActionResult Sandbox(int? parameter)
        {
            Models.Pages.Blog.Sandbox page = new Models.Pages.Blog.Sandbox();
            Int32? UserID = Wild2.Models.Auth.AuthOnSite.GetUserId();
            //******************************************************
            if (UserID.HasValue)
            {
                Models.Favorites.Favorites.AddFavoriteToSession("Песочница");
                Wild2.Models.ViewTabs.ViewTabs.setCurrentTab(Url.RequestContext.RouteData.Values["controller"].ToString(), Url.RequestContext.RouteData.Values["action"].ToString());
            }
            Wild2.Models.Pages.Controls.PagingTop pt = new Wild2.Models.Pages.Controls.PagingTop();
            pt.PreviousLink = Url.Action("All", "Blog", new { parameter = 0 });
            pt.PreviousName = "Все записи";
            pt.CurrentName = "Песочница";
            if (UserID.HasValue)
            {
                pt.NextLink = Url.Action("My", "Blog", new { parameter = 0 });
                pt.NextName = "Мои записи";
            }
            else
            {
                pt.NextLink = Url.Action("Index", "Home", new { parameter = 0 });
                pt.NextName = "Популярные";
            }
            page.pt = pt;
            //******************************************************

            //******************************************************
            Wild2.Models.Pages.Controls.BlogsTopicList btl = new Models.Pages.Controls.BlogsTopicList();
            if (parameter.HasValue) btl.TopicList = Models.Blog.Blog.GetTopicsSandbox(parameter.Value);
            else btl.TopicList = Models.Blog.Blog.GetTopicsSandbox(1);
            page.btl = btl;
            //******************************************************

            //******************************************************
            Wild2.Models.Pages.Controls.PagingBottom pb = new Wild2.Models.Pages.Controls.PagingBottom();
            if (parameter.HasValue)
            {
                pb.PreviousLink = Url.Action("Sandbox", new { parameter = parameter + 1 });
                if (parameter > 0)
                {
                    pb.NextLink = Url.Action("Sandbox", new { parameter = parameter - 1 });
                }
            }
            else
            {
                pb.PreviousLink = Url.Action("Sandbox", new { parameter = 1 });
            }
            page.pb = pb;
            //******************************************************

            return View(page);
        }

        public ActionResult Tags()
        {
            return View();
        }
    }
}
